/**
 * 
 */
package messageAPI.RabbitMQIntegration;

import java.io.IOException;


import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
/**
 * The Class RabbitMQBase.
 *
 * @author Elmar
 */


class RabbitMQBase {
	
	public static String Server;
	public static String ExchangeName;
	public static String ExchangeTypeName;
	public static String RoutingKey;
    
  //private static boolean isConsuming;
	public static boolean isDurable;
	public static String QUEUE_NAME;
    
    public static Connection RabbitConnection;
    public static Channel RabbitChannel;

	   private static RabbitMQBase instance = null;
	   
   	/**
   	 * Instantiates a new rabbit mq base.
   	 */
   	protected RabbitMQBase() {
		// Exists only to defeat instantiation.
		   Server="localhost";
	        ExchangeName="perfdataexchange";
	        ExchangeTypeName="direct";
	        isDurable=true;
	        QUEUE_NAME="javamessageapiqueue";
	        
	        
	        ConnectionFactory factory = new ConnectionFactory();
	        factory.setHost(Server);
	        try {
				RabbitConnection = factory.newConnection();
			} catch (IOException e) {
                // TODO Auto-generated catch block
				e.printStackTrace();
			}
	        try {
				RabbitChannel = RabbitConnection.createChannel();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

	        try {
				RabbitChannel.exchangeDeclare(ExchangeName, ExchangeTypeName, isDurable);
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}      
	        
	      
	   }
	   
   	/**
   	 * Gets the single instance of RabbitMQBase.
   	 *
   	 * @return single instance of RabbitMQBase
   	 */
   	public static RabbitMQBase getInstance() {
	      if(instance == null) {
	         instance = new RabbitMQBase();
	      }
	      return instance;
	   }
}

//OLD CODE: MUST BE REMOVED ONCE INTEGRATION TESTS ARE SIGNED OFF
//
//public class RabbitMQBase {
//
//	
//	public static String Server;
//	public static String ExchangeName;
//	public static String ExchangeTypeName;
//	public static String RoutingKey;
//    
//  //private static boolean isConsuming;
//	public static boolean isDurable;
//  //  private static boolean autoAck;
//  //private static String QueueName;
//	public static String QUEUE_NAME;
//    
//    public Connection RabbitConnection;
//    public Channel RabbitChannel;
//	
//	public RabbitMQBase() throws Throwable
//	{
//    	Server="localhost";
//        ExchangeName="perfdataexchange";
//        ExchangeTypeName="direct";
//        isDurable=true;
//    //    autoAck=true;
//        QUEUE_NAME="javamessageapiqueue";
//      
////    	String propertyNames[] = new String[] { "Server", "ExchangeName", "ExchangeTypeName", "isDurable", "autoAck", "QUEUE_NAME"};
////    	 
////    	// convert array to list
////    	List<String> propertyList = Arrays.asList(propertyNames);
////    	
////    	
////    	propertyList.add("Server");
////    	propertyList.add("ExchangeName");
////    	propertyList.add("ExchangeTypeName");
////    	propertyList.add("isDurable");
////    	propertyList.add("autoAck");
////    	propertyList.add("QUEUE_NAME");
////    	
////    	Map<String,String> configuredProperties=Configuration.GetProperties(propertyList);
////    	
////    	Server=configuredProperties.get(Server);
////    	Server=configuredProperties.get(ExchangeName);
////        ExchangeName=configuredProperties.get(ExchangeTypeName);
////        ExchangeTypeName=configuredProperties.get(isDurable);
////        RoutingKey=configuredProperties.get(autoAck);
////        QUEUE_NAME=configuredProperties.get(QUEUE_NAME);
//		
//		
//		
//    	ConnectionFactory factory = new ConnectionFactory();
//        factory.setHost(Server);
//        RabbitConnection = factory.newConnection();
//        RabbitChannel = RabbitConnection.createChannel();
//
//        RabbitChannel.exchangeDeclare(ExchangeName, ExchangeTypeName, isDurable);                
//
//	}
//	
//}
